
#include <apr_time.h>
#include "../iface/APRInterface.h"
#include "../utils/StringUtils.h"
#include "XMLWriter.h"

XMLWriter::XMLWriter()
{
   
}

XMLWriter::~XMLWriter()
{
   
}

int XMLWriter::XMLWrite(const char *filename, xmlDocPtr tdoc)
{
   apr_status_t status;
   apr_pool_t *mp;
   status = apr_pool_create(&mp, NULL);
   CheckAPRError(status);
#ifdef XML_SAVE_WSNONSIG
   xmlSaveCtxtPtr svectxt = xmlSaveToFilename(StringUtils::expandMyHomeDirectoryTilde(filename, mp), NULL, XML_SAVE_WSNONSIG);
#else
   xmlSaveCtxtPtr svectxt = xmlSaveToFilename(StringUtils::expandMyHomeDirectoryTilde(filename, mp), NULL, 0);
#endif
   apr_pool_destroy(mp);
   xmlSaveDoc(svectxt, tdoc);
   int retval = 0, retries = 3;
   while ((retval = xmlSaveFlush(svectxt)) && (retries--)) apr_sleep(1000);
   return (xmlSaveClose(svectxt) < 0) - retval;
}

int XMLWriter::XMLWrite(xmlBufferPtr buffer, xmlDocPtr tdoc)
{
#ifdef XML_SAVE_WSNONSIG
   xmlSaveCtxtPtr svectxt = xmlSaveToBuffer(buffer, NULL, XML_SAVE_WSNONSIG);
#else
   xmlSaveCtxtPtr svectxt = xmlSaveToBuffer(buffer, NULL, 0);
#endif
   xmlSaveDoc(svectxt, tdoc);
   return (xmlSaveClose(svectxt) < 0);
}
